package com.atguigu.gmall.seckill.consumer;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.model.activity.SeckillGoods;
import com.atguigu.gmall.seckill.mapper.SeckillGoodsMapper;
import com.atguigu.gmall.seckill.mapper.SeckillOrderMapper;
import com.atguigu.gmall.seckill.pojo.SeckillOrder;
import com.atguigu.gmall.seckill.pojo.UserRecode;
import com.atguigu.gmall.seckill.service.SecKillService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Map;

/**
 * @author wjh
 * @version 1.0
 * @date 2021/7/18 23:24
 * 秒杀订单的监听类
 */
public class SeckillOrderUpdateListener {
    @Autowired
    private SecKillService secKillService;

    /**
     * 监听发生了支付行为的秒杀订单，消息的生产者是支付微服务
     * @param msg
     */
    @RabbitListener(queues = "seckill_queue")
    public void updateSeckillOrder(String msg){
        //将消息的类型转换为map类型的数据
        Map<String,String> payResult = JSONObject.parseObject(msg, Map.class);
        String payway = payResult.get("payway");
        //1为支付宝 0 为微信
        if (payway.equals("0")){
            //支付宝直布罗基
            Map<String,String> map = JSONObject.parseObject(msg, Map.class);
            //进行解析，获取请求的结果
            String tradeStatus = map.get("trade_status");
            if (!StringUtils.isEmpty(tradeStatus) && tradeStatus.equals("TRADE_SUCCESS")){
                //支付成功，修改订单的状态
                secKillService.paySuccess(map);
                return;
            }
            //支付失败，修改订单的状态
            secKillService.payFail(map);
        }else {
            //微信支付逻辑
            String returnCode = payResult.get("return_code");
            if (returnCode.equals("SUUCESS")){
                //获取交易的结果
                String resultCode = payResult.get("result_code");
                if (resultCode.equals("SUCCESS")){
                    //支付成功。修改订单的状态
                    secKillService.paySuccess(payResult);
                    return;
                }
            }
            //支付失败，修改订单的状态
            secKillService.payFail(payResult);
        }

    }
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SeckillOrderMapper seckillOrderMapper;
    /**
     * 监听超时未支付的订单的延迟消息：秒杀的下单服务发送的消息
     * @param msg
     */
    public void cancelSeckillOrder(String msg){
        //数据类型的转换
        SeckillOrder seckillOrder = JSONObject.parseObject(msg, SeckillOrder.class);
        //取消订单
        //判断前置状态
        if (seckillOrder != null && seckillOrder.getStatus().equals("0")){
            //修改订单的支付状态
            seckillOrder.setStatus("3");
            //修改redis中订单的状态
            String username = seckillOrder.getUserId();
            redisTemplate.boundHashOps("SeckillOrder_" + username).put(seckillOrder.getId(), seckillOrder);
            //将订单的信息存入数据库
            seckillOrderMapper.insert(seckillOrder);
            //库存回滚---1.redis回滚  2.数据库回滚
            UserRecode userRecode = (UserRecode)redisTemplate.boundValueOps("SeckillStatus_" + username).get();
            rollBackSeckillGoodsNum(userRecode.getGoodsId(), userRecode.getTime(), userRecode.getNum());
            //排队标识删除

            //排队状态删除
        }
    }

    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;
    /**
     *
     * 回滚秒杀商品的库存
     * @param skuId
     * @param time
     * @param num
     */
    private void rollBackSeckillGoodsNum(String skuId, String time, Integer num) {
        //从redis中获取商品的数据
        SeckillGoods seckillGoods = (SeckillGoods)redisTemplate.boundHashOps("SeckillGoods_" + time).get(skuId);
        if (seckillGoods == null){
            //商品已经售罄了--更新数据库中的库存数据
           seckillGoodsMapper.updateSeckillGoodsStock(skuId,num);
        }else {
            //redis中还存在数据 需要更新
           String[] seckillGoodsQueue =  getSeckillGoodsQueue(skuId,num);
           redisTemplate.boundListOps("seckillGoodsQueue_" + skuId).leftPushAll(seckillGoodsQueue);
            //回滚库存的对象
            redisTemplate.boundHashOps("SeckillGoodsStockNum").increment(skuId,num);
        }
        
    }

    /**
     * 构建航拍库存个数长度的数组
     * @param goodsId
     * @param stockCount
     */
    private String[] getSeckillGoodsQueue(String goodsId, Integer stockCount) {
        //构建一个库存长度的数组
         String[] ids = new String[stockCount];
        //元素填充
        for (int i = 0; i < stockCount; i++) {
            ids[i] = goodsId;
        }
        //返回数据
        return ids;

    }

}
